//
//  BQCollectionViewController.m
//  BeeQuick
//
//  Created by mac on 16/11/20.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQCollectionViewController.h"
#import "BQHomeFlowLayout.h"
#import "CZAdditions.h"
#import "DSHTTPClient.h"
#import "BQCycleViewCell.h"
#import "BQFourButtonCell.h"
#import "BQHomeScrollCell.h"
#import "BQHomeGoodsTopCellCollectionViewCell.h"
#import <UIImageView+WebCache.h>


#import <YYModel.h>

#import "BQActRowModel.h"
#import "BQActivityModel.h"
#import "BQHomeGoodModel.h"
#import "BQCategoryDetailModel.h"
#import "BQHomeSellTopCell.h"
#import "BQHomeGoodsCell.h"



typedef enum : NSUInteger {
    modelType_focus = 0,
    modelType_icon = 1,
    modelType_scroll = 3,
//    modelType_firstSell = 4,
    modelType_goods = 5,
} modelType;




static NSString *cellID = @"cellID";
static NSString *cycleCell = @"cycleCell";
static NSString *fourBtnCell = @"fourBtnCell";
static NSString *scollCell = @"scrollCell";
static NSString *HomeSellTopCell = @"BQHomeSellTopCell";
static NSString *GoodsTopCell = @"GoodsTopCellCollectionViewCell";
static NSString *HomeGoodsCell = @"HomeGoodsCell";


@interface BQCollectionViewController ()
<
UICollectionViewDelegateFlowLayout,
BQCycleViewCellDelegate,
BQHomeGoodsCellDelegate
//,CAAnimationDelegate
>
@property(strong,nonatomic)NSArray<BQActRowModel *> *iconList;

@property(strong,nonatomic) NSArray<BQActRowModel *> *focusList;

@property(strong,nonatomic)NSArray<BQActRowModel*>* scorllList;

//@property(strong,nonatomic)NSArray<BQHomeFirstSellModel *> *sellList;

@property(strong,nonatomic)NSArray<BQActRowModel*> *goodsList;


@property(strong,nonatomic)NSArray<NSArray<BQActRowModel*>*>* bigModelList;
@end

@implementation BQCollectionViewController
{
   
    
    NSArray <NSString *>*_imgArr;
    
    NSArray <NSString *> *_titleArr;
    
    NSArray <NSString *>*_secondImageArr;
    
}

- (instancetype)init
{
    
    return [super initWithCollectionViewLayout:[[BQHomeFlowLayout alloc]init]];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //MARK: - 加载网络数据.
    [self loadData];
    
    self.collectionView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    
    
    //MARK: - 注册.
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    
    
    //第0组
    [self.collectionView registerClass:[BQCycleViewCell class] forCellWithReuseIdentifier:cycleCell];
    //第0组
    [self.collectionView registerClass:[BQFourButtonCell class] forCellWithReuseIdentifier:fourBtnCell];
    //第1组
  
    [self.collectionView registerClass:[BQHomeScrollCell class] forCellWithReuseIdentifier:scollCell];
    
    //第2组
    
    [self.collectionView registerClass:[BQHomeSellTopCell class] forCellWithReuseIdentifier:HomeSellTopCell];
    
    //第3组第0行
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"BQHomeGoodsTopCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:GoodsTopCell];

    [self.collectionView registerNib:[UINib nibWithNibName:@"BQHomeGoodsCell" bundle:nil] forCellWithReuseIdentifier:HomeGoodsCell];
    
    
}


#pragma mark - 请求数据
- (void)loadData
{
    
    //MARK: - plist 数据解析.
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"home.json" withExtension:nil];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
    
    NSDictionary * infoDic = dataDic[@"data"];
    
    NSArray *actInfoArr = infoDic[@"act_info"];
    
    //    NSLog(@"%@",actInfoArr);
    
    
    //这个是最顶部的轮播的 model数组  第0 组
    NSArray <BQActRowModel*>*focusModelArr = [NSArray yy_modelArrayWithClass:[BQActRowModel class] json:actInfoArr[0][@"act_rows"]];
    
    
    _focusList = focusModelArr;
    
    //    NSLog(@"%@",focusModelArr[0].activity.img);
    
    //图文混排的 model 第二行 0 组
    
    NSArray <BQActRowModel *> *iconModelArr = [NSArray yy_modelArrayWithClass:[BQActRowModel class] json:actInfoArr[modelType_icon][@"act_rows"]];
    
    self.iconList = iconModelArr;
    //    NSLog(@"%@",iconModelArr[0].activity.name);
    
    
//    第1组数据
    NSArray <BQActRowModel *> *scrollModelArr = [NSArray yy_modelArrayWithClass:[BQActRowModel class] json:actInfoArr[modelType_scroll][@"act_rows"]];
    
    
    self.scorllList = scrollModelArr;
    
//        NSLog(@"%@",scrollModelArr[0].activity.name);
    
    //第2组数据
//    NSArray <BQActRowModel *> *firstSellModelArr = [NSArray yy_modelArrayWithClass:[BQActRowModel class] json:actInfoArr[modelType_firstSell][@"act_rows"]];
    
    //    NSLog(@"%@",firstSellModelArr[0].act_rows[0]);
    
    
    
    
    
    
    //第3组商品
    NSArray <BQActRowModel *> *goodsModelArr = [NSArray yy_modelArrayWithClass:[BQActRowModel class] json:actInfoArr[modelType_goods][@"act_rows"]];
    
    
    self.goodsList = goodsModelArr;
    
    
    
    //    NSLog(@"%@",goodsModelArr);
    
//    BQHomeGoodModel *model = goodsModelArr[0].category_detail.goods[0];
    
//    NSLog(@"%@",model);
    
///MARK: - 赋值完重新刷新.
    
    
        NSArray *titleArr = @[@"优选水果",@"牛奶面包",@"饮料酒水",@"更多"];
    
//    self
    
        NSArray *imgArr = @[@"youxuanshuiguo",@"niunaimianbao",@"yinliaojiushui",@"more"];
    
    
    _titleArr = titleArr;
    
    _imgArr = imgArr;
    
   NSArray * imgSecondArr = @[@"business1",@"business2",@"business3",@"business4"];
    
    _secondImageArr = imgSecondArr;
    
    [self.collectionView reloadData];
    
    
    
    
//    @property(strong,nonatomic)NSArray<BQActRowModel *> *iconList;
//    
//    @property(strong,nonatomic) NSArray<BQActRowModel *> *focusList;
//    
//    @property(strong,nonatomic)NSArray<BQActRowModel*>* scorllList;
//    

//    
//    @property(strong,nonatomic)NSArray<BQActRowModel*> *goodsList;

    NSArray *BQHomeBigArr = @[self.iconList,self.focusList,self.scorllList,self.goodsList];
    self.bigModelList = BQHomeBigArr;
    
    if([self.delegate respondsToSelector:@selector(transfromMyBigModel:)])
    {
        [self.delegate transfromMyBigModel:self.bigModelList];
    }
    
    
}

#pragma mark - collectionView的数据源方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    //先算3组
    return 7;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    switch (section) {
        case 0://第0组
            return 2;
        case 1:
            return 1;
        case 2:
            return 6;
        case 3:
        case 4:
        case 5:
        case 6:
            return 4;
    }
    
    return 0;
    
}

//MARK: - 数据源方法三.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    
    
    NSString * reuseId = cellID;
    
    if(indexPath.section == 0)
    {
        
        reuseId = cycleCell;
        
        if(indexPath.row == 0)
        {
            BQCycleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
            
            cell.focusModelArr = self.focusList;
            //MARK: - 设置第0组滚动图片的代理.
            cell.delegate = self;
            return cell;
        }
        else
        {
            BQFourButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fourBtnCell forIndexPath:indexPath];
            
            if(self.iconList)
            {
                cell.modelArr = self.iconList;
            }
            return cell;
        }
        
    }else if (indexPath.section == 1)
    {
        
        BQHomeScrollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:scollCell forIndexPath:indexPath];
        
        cell.scrollArr = self.scorllList;
        
        
        return cell;
    }else if (indexPath.section == 2)
    {
        if(indexPath.row == 0)
        {
            BQHomeSellTopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeSellTopCell forIndexPath:indexPath];
            
            return cell;
            
        }
        if(indexPath.row == 1)
        {
            BQFourButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fourBtnCell forIndexPath:indexPath];
            
            [cell setButtonWithImgArr:_imgArr andTitleArr:_titleArr];
            
            
            return cell;
        }else
        {
            
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
            
            UIImageView *pic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,(kScreenW-1)*1.0/2 , 70)];
            
            pic.image = [UIImage imageNamed:_secondImageArr[(indexPath.row - 2)]];
            
            cell.backgroundView = pic;
            
            return cell;
            
        }
        
    }
    if(indexPath.section > 2)
    {
        if(indexPath.row == 0)
        {
            BQHomeGoodsTopCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GoodsTopCell forIndexPath:indexPath];

            cell.model = self.goodsList[indexPath.section - 3];
            
            return cell;
        }
        else
        {
            BQHomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeGoodsCell forIndexPath:indexPath];
            
            cell.delegate = self;
            
            cell.model = self.goodsList[indexPath.section-3].category_detail.goods[indexPath.row-1];
            
            return cell;
            
        }
    }
    
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            return CGSizeMake(kScreenW, 160);
        }else
        {
            return CGSizeMake(kScreenW, 80);
        }
    }
    else if (indexPath.section == 1)
    {
        return CGSizeMake(kScreenW, 140);
    }
    else if (indexPath.section == 2)
    {
        if(indexPath.row == 0)
        {
        return CGSizeMake(kScreenW, 130);
        }
       else if(indexPath.row == 1)
        {
            return CGSizeMake(kScreenW, 85);
        }
        else
        {
            return CGSizeMake((kScreenW / 2 -0.5), 70);
        }
    }
    else if(indexPath.section >= 3 && indexPath.row == 0)
    {
        return CGSizeMake(kScreenW, 130);
    }
    else
    {
        return CGSizeMake((kScreenW -2)/3, 205);
    }
    
    
}


#pragma mark - 分别修改 每一组的 item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if(section == 0)
    {
        return 0.0;
    }
    if(section == 2)
    {
        return 1/kScale;
    }
    
    if(section >= 3)
    {
        return 1/kScale;
    }
    return 0;
    
    
}

- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if(section == 0)
    {
        return 0;
    }
    if(section == 2)
    {
        return 1/kScale;
    }
    
    return 0;
}



#pragma mark - collectionView item 选中时间
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 1)
    {
        //MARK: - 取出第0组第1行数据的model.
        
        BQActRowModel *model = self.iconList[indexPath.row];
        
        NSLog(@" %@ ",model.activity.name);
        
    }
}


#pragma mark - 实现轮播 cell 代理的方法

- (void)BQCycleViewCell:(BQCycleViewCell *)cell withPicIndex:(NSInteger)index
{
    //先根据下标取出model
    
    BQActRowModel *model = self.focusList[index];
    
    
    NSLog(@"我是轮播第%zd张图, %@",index,model.activity.name);
    
    
}



#pragma mark - 实现点击动画方法
- (void)BQHomeGoodsCellWithStartPoint:(CGPoint)startP andModel:(BQHomeGoodModel *)model
{
    
    UIImageView *picView = [[UIImageView alloc]init];
    
    NSURL *url = [NSURL URLWithString:model.img];
    
    picView.bounds = CGRectMake(0, 0, 50, 50);
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    //给picView设置图片
    [picView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        //设置图片完成后
        //把动画图片添加到window
        [keyWindow addSubview:picView];
        
        //关键帧动画
        CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        //起点
        [path moveToPoint:startP];
        
        //终点
        CGPoint endPoint = CGPointMake(kScreenW * 0.65, kScreenH - 46);
        
        //路径
        [path addLineToPoint:endPoint];
        
        anim.path = path.CGPath;
        
        //时间
        anim.duration = 0.6;
        
        [UIView animateWithDuration:0.5 animations:^{
            
            //缩放
            picView.transform = CGAffineTransformMakeScale(0.2, 0.2);
            
        }];
        
        
        anim.delegate = self;
        
        [anim setValue:picView forKey:@"endImage"];
        
        [picView.layer addAnimation:anim forKey:@"keyAnim"];
        
        
        
        
    }];
    
    
    
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    UIImageView *endView = [anim valueForKey:@"endImage"];
    
    [endView removeFromSuperview];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    
    CGFloat alpha = (offset.y + 20) / 96;
    
    
    
    if([self.delegate respondsToSelector:@selector(showMyScollcontentoffSizeY:andOffSetY:withAlpha:)])
    {
        [self.delegate showMyScollcontentoffSizeY:self andOffSetY:offset.y withAlpha:alpha];
    }
    
//    UIColor *color = nil;
//    color = alpha > 1 ? [UIColor blackColor] : [UIColor whiteColor];
    
    
    
}





@end
